//
//  CTPerformanceVC.m
//  digitalCurrency
//
//  Created by apple on 2019/6/12.
//  Copyright © 2019 XinHuoKeJi. All rights reserved.
//

#import "CTPerformanceVC.h"
//
#import "CEPintHeaderSuperView.h"
#import "CEPointInsCell.h"
#import "CEPointListController.h"
#import "CTPerformentModel.h"
#import "MineNetManager.h"

@interface CTPerformanceVC()
@property (nonatomic, strong) CEPintHeaderSuperView *headerview;
@property (nonatomic, strong) NSMutableArray *pointInsList;
@property (nonatomic, strong) LYEmptyView *emptyView;
@property (nonatomic, copy) NSString *referTeamAsset;
@property (nonatomic, assign) NSInteger page;

@end

@implementation CTPerformanceVC

- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 1;
    
    self.viewTitle.text = LocalizationKey(@"Revenue sharing");
    self.pointInsList = [NSMutableArray array];
    
    [self.view addSubview:self.tableView];
    self.tableView.separatorColor  = [UIColor clearColor];
    self.tableView.tableHeaderView = self.headerview;
    
    [self headRefreshWithScrollerView:self.tableView];
    [self footRefreshWithScrollerView:self.tableView];
    
    [self getInvestedIncomeLogs];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    self.tableView.frame = self.view.bounds;
}

- (LYEmptyView *)emptyView {
    if (!_emptyView) {
        _emptyView = [LYEmptyView emptyViewWithImageStr:@"emptyData" titleStr:[[ChangeLanguage bundle] localizedStringForKey:@"NoData" value:nil table:@"English"]];
        _emptyView.titleLabTextColor = tThreeColor;
        _emptyView.titleLabFont = [UIFont systemFontOfSize:19];
        _emptyView.contentViewY = SCREEN_HEIGHT/2.0-20;
    }
    return _emptyView;
}



- (void)getInvestedIncomeLogs {
    [MineNetManager getInvestedIncomeLogs:
                    @{@"userId":[YLUserInfo shareUserInfo].ID,
                        @"unit":@"MOS",@"page":@(_page),@"row":@(20)
                        } CompleteHandle:^(id resPonseObj, int code) {
        if (code) {
            NSInteger codeInt = [resPonseObj[@"code"] integerValue];
            NSDictionary *data = resPonseObj[@"data"];
            NSArray *restList = data[@"restList"];
            self.headerview.allcountLabel.text  = [data[@"totalCnt"] stringValue];

            if (codeInt == 0) {
                
                if (_page == 1) {
                    [self.pointInsList removeAllObjects];
                }
                else {
                    if (restList.count == 0) {
                        self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
                    }
                }
                
                for (NSDictionary *subdic in restList) {
                    [self.pointInsList addObject:subdic];
                }
                
                self.tableView.ly_emptyView = self.emptyView;
                [self.tableView reloadData];
            }
            else {
                [self.view makeToast:resPonseObj[MESSAGE] duration:1.5 position:CSToastPositionCenter];
            }
            
        }
        else {
            [self.view makeToast:LocalizationKey(@"noNetworkStatus") duration:1.5 position:CSToastPositionCenter];
        }
    }];
}

//MARK:--下拉刷新
- (void)refreshHeaderAction {
    _page = 1;
    [self getInvestedIncomeLogs];
}

- (void)refreshFooterAction {
    _page++;
    [self getInvestedIncomeLogs];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.pointInsList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"defaultCell"];
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"defaultCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.textColor = tThreeColor;
        cell.detailTextLabel.textColor = [UIColor colorWithHEX:maincolor];
    }
    
    NSDictionary *subdic = self.pointInsList[indexPath.row];
    cell.textLabel.text = [ToolUtil timeIntervalToTimeString:subdic[@"time"] WithDateFormat:@"YY-MM-dd HH:mm:ss"];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"+%@ MOS",[ToolUtil judgeStringForDecimalPlaces:[NSString stringWithFormat:@"%@",subdic[@"amount"]] point:4]];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CEPintHeaderSuperView *)headerview {
    if (!_headerview) {
        _headerview = [[CEPintHeaderSuperView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 190)];
        _headerview.headTitle.text = LocalizationKey(@"Total sharing");
        _headerview.backgroundColor = [UIColor clearColor];
        _headerview.titles = @[@"",@"",@""];
    }
    
    return _headerview;
}




@end
